package com.gnerv.battle.boot.project.guard.service.impl;

import com.gnerv.battle.boot.common.tools.command.ExecResult;
import com.gnerv.battle.boot.common.tools.command.SftpTools;
import com.gnerv.battle.boot.common.tools.command.ShellTools;
import com.gnerv.battle.boot.project.guard.model.entity.MonitorHost;
import com.gnerv.battle.boot.project.guard.service.MonitorDeployService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorDeployServiceImpl implements MonitorDeployService {

    @Override
    public boolean deploy(MonitorHost monitorHost, String destination) {
        // 判断操作系统和cpu架构
        // 创建部署监控采集工具目录
        // 添加执行权限
        SftpTools.upload(new File("/Users/ligen/Downloads/telegraf-1.30.2 2/usr/bin/telegraf"),
                new File(destination), monitorHost.getUsername(), monitorHost.getPassword(), monitorHost.getHost(), monitorHost.getPort());
        return false;
    }

    @Override
    public boolean deploy(MonitorHost monitorHost, String source, String target) {
        return false;
    }

    @Override
    public boolean undeploy(MonitorHost monitorHost, String path) {
        return false;
    }

    @Override
    public boolean start(MonitorHost monitorHost, String path, String url) {
        String command = "nohup " + path + "  --config " + url + " > /root/soft/aaaaa.log 2>&1 &";
        ShellTools.exec(command, monitorHost.getUsername(), monitorHost.getPassword(), monitorHost.getHost(), monitorHost.getPort());
        return true;
    }

    @Override
    public boolean stop(MonitorHost monitorHost, String path) {
        return false;
    }
}
